/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.core.service.base.impl;

import cn.simastudio.talos.common.constants.SimaConstants;
import cn.simastudio.talos.core.exception.biz.ConstraintCheckException;
import cn.simastudio.talos.core.exception.biz.DataDeleteException;
import cn.simastudio.talos.core.exception.biz.DataInsertException;
import cn.simastudio.talos.core.exception.biz.DataUpdateException;
import cn.simastudio.talos.core.mapper.base.ResourceMapper;
import cn.simastudio.talos.core.mapper.base.RoleResRelMapper;
import cn.simastudio.talos.core.model.base.Resource;
import cn.simastudio.talos.core.model.base.RoleResRel;
import cn.simastudio.talos.core.service.base.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("SpringJavaAutowiringInspection")
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private RoleResRelMapper roleResRelMapper;

    @Override
    public Set<String> getPermissions(String username) {
        List<Resource> resourceList = resourceMapper.getPermissionsByUsername(username);
        return resourceList.stream().map(Resource::getPermission).collect(Collectors.toSet());
    }

    @Override
    public Resource createResource(Resource resource) {
        int createRes = resourceMapper.insertSelective(resource);
        if (createRes != SimaConstants.DEFAULT_UPDATE_COUNT) {
            throw new DataInsertException();
        }
        return resource;
    }

    @Override
    public boolean updateResource(Resource resource) {
        int updateRes = resourceMapper.updateByPrimaryKeySelective(resource);
        if (updateRes != SimaConstants.DEFAULT_UPDATE_COUNT) {
            throw new DataUpdateException();
        }
        return true;
    }

    @Override
    public boolean deleteResource(Integer resId) {
        List<RoleResRel> roleResRelList = roleResRelMapper.getRoleResRelListByResId(resId);
        if (roleResRelList.size() > 0) {
            throw new ConstraintCheckException();
        }
        int deleteRes = resourceMapper.deleteByPrimaryKey(resId);
        if (deleteRes != SimaConstants.DEFAULT_DELETE_COUNT) {
            throw new DataDeleteException();
        }
        return true;
    }

    @Override
    public Resource getResource(Integer resId) {
        return resourceMapper.selectByPrimaryKey(resId);
    }

    @Override
    public List<Resource> getAllResources() {
        return resourceMapper.getAllResource();
    }


}
